package demo;

import java.util.List;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;

import weka.classifiers.functions.LibSVM;
import weka.classifiers.Classifier;
import weka.core.Instances;
import algorithms.AWE;
import algorithms.SVM;
import evaluation.Measure;
import util.Utils;

public class SVMDemo2 {

	static int file_idx = 0;
	static List<String> files = null;
	
	public static void main(String[] args) throws Exception{

		String list = "data/dataset/moorelist.txt";
		Instances[] datasets = getdatasets(list);
		String models = "data/models/model.list";
		
		LibSVM learner = new LibSVM();
		AWE awe = new AWE(datasets, learner);
		awe.setMaxEnsembleSize(5);
		awe.setNumberOfFolds(5);

		List<String> classifiers = new ArrayList<String>();
		BufferedReader reader = new BufferedReader(new FileReader(models));
		while(reader.ready()){
			classifiers.add(reader.readLine());	
		}
		reader.close();
		Classifier x = SVM.loadModel(classifiers.get(classifiers.size()-1));
		awe.addCurrentClassifier(x);
		System.out.println("Current Classifier Added");
		for(int i = 0; i < classifiers.size()-1; ++i ){
			Classifier p = SVM.loadModel(classifiers.get(i));
			awe.addClassifier(p);
			System.out.println("One Classifier Added");
        }
        
//		awe.process();
		awe.information();
		
		System.out.println("Start Evaluation");
		Measure mea = new Measure(datasets[datasets.length-1]);
		mea.evaluate(awe);
		
		
	}
	
	static Instances[] getdatasets(String list) throws Exception{
		BufferedReader reader = new BufferedReader(new FileReader(list));
		files = new ArrayList<String>();
		while(reader.ready()){
			String line = reader.readLine();
			if(!line.startsWith("#")){
			  files.add(line);
			}
		}
		Instances[] datasets = new Instances[files.size()];
		for(int i = 0; i < files.size(); ++i){
			datasets[i] = Utils.getInstances(files.get(i)); 
		}
		return datasets;
	}
	
}
